﻿package 
{
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageQuality;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import samples.BaseSample;
	import samples.d2.polygons.Polygon0;
	import samples.d2.shapes.*;
	import samples.d2.trames.*;
	import shapes.shapes2D.distribution.tiling.*;
	
	
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Shapes extends Sprite 
	{
		
		private var sample:BaseSample;
		private var sampleId:int = 0;
		private var tf:TextFormat;
		private var txt:TextField;
		private var swap:Boolean = false;
		
		public function Shapes():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_stage = stage;
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			_stage.displayState = StageDisplayState.FULL_SCREEN;
			
			_stage.addEventListener( KeyboardEvent.KEY_DOWN, keyHandler )
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			
			tf = new TextFormat( 'Verdana', 16, 0xFFFFFF );
			
			sampleId = 0;
			keyHandler();
			
		}
		
		private function keyHandler( e:KeyboardEvent = null ):void 
		{
			if (	e != null
				&&	e.keyCode != Keyboard.RIGHT 
				&& 	e.keyCode != Keyboard.LEFT 
				&&	e.keyCode != Keyboard.UP ) return;
			
			if ( e != null )
			{
				switch( e.keyCode )
				{
					case Keyboard.RIGHT:
					sampleId++;
					break;
					
					case Keyboard.LEFT:
					sampleId--;
					break;
					
				}
			}
			if ( sampleId > 37 ) sampleId = 37;
			if ( sampleId < 0) sampleId = 0;
			var n:int = numChildren;
			
			while ( n-- )
			{
				if( !( getChildAt( n ) is TextField ) ) removeChildAt( n );
			}
			
			var op:int = 0;
			var description:String = '';
			switch( sampleId )
			{
				
				case op++:
					description = 'PRS:\nposition: X/Y\nrotation: A\nscale: SX/SY\nskew: SX/SY';
					txt.text = sampleId.toString() + ' : ' +description;
					txt.setTextFormat( tf );
					addChild( new PRS() );
				return;
				
				case op++:
					description = 'a circle';
					sample = new Circle0();
				break;
				
				
				case op++:
					description = 'distributing circles';
					sample = new Circle1();//trame
				break;
				
				
				case op++:
					description = 'random size circle evenly distributed';
					sample = new Circle2();//random sized trame
				break;
				
				case op++:
					description = 'hexagonal distribution';
					sample = new Circle3();//trame hexagonale
				break;
				
				case op++:
					description = 'jittered hexagonal distribution';
					sample = new Circle4();//trame hexa jittered
				break;
				
				//variation sur l' organisation des points en cercle
				case op++:
					description = 'variation on circular distribution #1 : ordered';
					sample = new CircularSample0();//en ordre
				break;
				
				case op++:
					description = 'variation on circular distribution #2 : shuffled';
					sample = new CircularSample1();//shuffle
				break;
				
				case op++:
					description = 'variation on circular distribution #3 : disorder + fill';
					sample = new CircularSample2();//desordre + remplissage 
				break;
				
				//distribution cicle packing
				case op++:
					description = 'circle packing';
					txt.text = sampleId.toString() + ' : ' +description;
					txt.setTextFormat( tf );
					addChild( new CirclePacking() );
					return;
					
					
				case op++:
					description = 'concentric circles';
					sample = new Circle5();//concentrique
				break;
				
				case op++:
					description = 'unbalanced concentric circles';
					sample = new Circle6();//concentrique desaxe
				break;
				
				case op++:
					description = 'sinusoidal concentric circles';
					sample = new Circle7();//concentrique sin
				break;
				
				case op++:
					description = 'unbalanced sine / cosine circles';
					sample = new Circle8();//concentrique desaxe + sin
				break;
				
				case op++:
					description = 'sine / cosine circles';
					sample = new Circle9();//concentrique cos / sin
				break;
					
					
				//un carre
				case op++:
					description = 'squares';
					sample = new Square0();
				break;
				
				case op++:
					description = 'grid distribution + recursive scale';
					sample = new Square1();//trame
				break;
				
				case op++:
					description = 'distribution box fitting ( love this one )';
					sample = new Rect1();
				break;
				
				case op++:
					description = 'sine / cosine distribution: maikng a curve with a square';
					sample = new Square2();//cos/sin
				break;
				
				case op++:
					description = 'animation sine / cosine';
					sample = new Square3();//anim: cos/sin
				break;
				
				case op++:
					description = 'shape based pattern';
					sample = new Square4();//motif
				break;
				
				case op++:
					description = 'shape based pattern: sine / cosine';
					sample = new Square5();//motif: cos/sin
				break;
				
				//case op++:
					//description = 'shape based pattern: mural';
					//sample = new Square6();//motif frise
				//break;
				
				case op++:
					description = 'hand ade shape based pattern: stripes';
					sample = new Square7();//motif stripe 
				break;
				
				case op++:
					description = 'shape based pattern: pattern parser';
					sample = new Square8();//parseur motif 
				break;
					
					
					//un rectangle
				case op++:
					description = 'rectangle ( aka Quad )';
					sample = new Rect0();
				break;
					
					
				//une forme composee
					
				case op++:
					description = 'compound shapes: 2 Quads > Cross';
					sample = new Cross0();
				break;
					
				case op++:
					description = 'cross + poisson distribtion';
					sample = new Cross1();
				break;
					
				case op++:
					description = 'or a filled polyline';
					sample = new Arc1();
				break;
				
				//un polygone regullier
				
				case op++:
					description = 'regular polygons';
					sample = new RegularPolygon0();
				break;
					
				case op++:
					description = 'regular poly + Poisson disk + soup';
					sample = new RegularPolygon1();// poissonDisk + soupe
				break;
					
				case op++:
					description = 'regular poly hexagonal distribution';
					sample = new RegularPolygon2( swap );//hexagons
					swap = !swap ? true : false;
				break;
					
				case op++:
					description = 'triangles';
					sample = new RegularPolygon3( swap );//triangles
					swap = !swap ? true : false;
				break;
				
				case op++:
					description = 'hexagonal distribution of triangles & pentagons';
					sample = new RegularPolygon4( swap );//distribution hexagonale triangles et pentagones
					swap = !swap ? true : false;
				break;
					
				case op++:
					description = 'contains test: it\'s almost like a circle distance';
					sample = new RegularPolygon5(); // contains test
				break;
					
				
			}
			
			txt.text = sampleId.toString() + ' : ' +description;
			txt.setTextFormat( tf );
			if( sample != null ) addChild( sample );
			
		}
	}
	
}